package com.ksyzt.ksite2.client.orgnization;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.uibinder.client.UiConstructor;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TreeItem;
import com.ksyzt.gwt.client.event.HasMessageHandlers;
import com.ksyzt.gwt.client.event.MessageEvent;
import com.ksyzt.gwt.client.event.MessageHandler;
import com.ksyzt.ksite2.client.common.ServerProxy;
import com.ksyzt.ksite2.shared.modle.ORGANIZATIONObj;
import com.ksyzt.ksite2.shared.modle.ORGANIZATIONObjs;

public class OrgnizationList extends ListBox implements HasMessageHandlers {

	@UiConstructor
	public OrgnizationList() {
		super();
	}

	List<ORGANIZATIONObj> m_list;
	private AsyncCallback<ORGANIZATIONObjs> on_root_data = new AsyncCallback<ORGANIZATIONObjs>() {

		public void onSuccess(ORGANIZATIONObjs result) {
			m_map.clear();
			GWT.log("receive root data:"+result.size());
			parseRoot(result);
		}

		public void onFailure(Throwable caught) {

			message(caught.getMessage());
		}
	};

	private void getChildItem(ORGANIZATIONObj o) {
		ServerProxy.SYS_MANAGER.getAllChildrens(o.getID(), on_all_chidren);
	}

	List<TreeItem> m_roots;

	private void parseRoot(ORGANIZATIONObjs result) {
		m_roots = new ArrayList<TreeItem>();
		if (result.size() > 0) {
			for (int i = 0; i < result.size(); i++) {
				ORGANIZATIONObj o = result.get(i);
				TreeItem ti = new TreeItem("0");
				ti.setUserObject(o);
				m_map.put(o.getID(), ti);
				m_roots.add(ti);
				getChildItem(o);
			}
		}
	}

	public ORGANIZATIONObj getSelectOrgnization() {
		int index = this.getSelectedIndex();
		if (index >= 0) {
			String v = this.getValue(index);
			Integer oid = Integer.valueOf(v);
			TreeItem ti = m_map.get(oid);
			if (ti != null) {
				return (ORGANIZATIONObj) ti.getUserObject();
			} else {
				return null;
			}
		} else {
			return null;
		}
	}

	void redraw(TreeItem item) {

		if (item == null) {
			for (int i = 0; i < m_roots.size(); i++) {
				TreeItem ti = m_roots.get(i);
				ORGANIZATIONObj o = (ORGANIZATIONObj) ti.getUserObject();

				int space = Integer.valueOf(ti.getHTML());
				String str = "";
				for (int j = 0; j < space; j++) {
					str += "  ";
				}
				this.addItem(str + o.getNAME(), o.getID() + "");

				for (int k = 0; k < ti.getChildCount(); k++) {
					TreeItem tic = ti.getChild(k);
					redraw(tic);
				}
			}
		} else {
			TreeItem ti = item;
			ORGANIZATIONObj o = (ORGANIZATIONObj) ti.getUserObject();

			int space = Integer.valueOf(ti.getHTML());
			String str = "";
			for (int j = 0; j < space; j++) {
				str += "  ";
			}
			this.addItem(str + o.getNAME(), o.getID() + "");

			for (int k = 0; k < ti.getChildCount(); k++) {
				TreeItem tic = ti.getChild(k);
				redraw(tic);
			}
		}
	}

	private AsyncCallback<ORGANIZATIONObjs> on_all_chidren = new AsyncCallback<ORGANIZATIONObjs>() {

		public void onSuccess(ORGANIZATIONObjs result) {

			for (int i = 0; i < result.size(); i++) {
				ORGANIZATIONObj o = result.get(i);
				TreeItem ti = m_map.get(o.getPID());

				int space = Integer.valueOf(ti.getHTML());
				if (ti != null) {
					TreeItem tic = ti.addItem("" + (space + 1));
					m_map.put(o.getID(), tic);
					tic.setUserObject(o);
				}
			}

			OrgnizationList.this.clear();
			redraw(null);
		}

		public void onFailure(Throwable caught) {
			message(caught.getMessage());

		}
	};

	
	Map<Integer, TreeItem> m_map = new HashMap<Integer, TreeItem>();

	int m_uid = -1;
	int m_type = -1;

	/**
	 * 初始化组织List
	 *
	 * @param userid
	 *            该组织树所属用户ID
	 * @param orgtype
	 *            要编辑的组织类型ID
	 */
	public void initList(int userid, int orgtype) {
		if (m_uid == userid && m_type == orgtype) {

		} else {
			m_uid = userid;
			m_type = orgtype;
			this.clear();
			ServerProxy.SYS_MANAGER.getOrganizations(userid, orgtype,
					on_root_data);
		}
	}

	protected void message(String message) {
		MessageEvent e=new MessageEvent(MessageEvent.MESSAGE,message);
		fireEvent(e);
		
	}

	public void refresh() {
		if (m_uid >= 0 && m_type >= 0) {
			this.clear();
			ServerProxy.SYS_MANAGER.getOrganizations(m_uid, m_type,
					on_root_data);
		}
	}

	@Override
	public HandlerRegistration addMessageHandler(MessageHandler handler) {
		return addHandler(handler, MessageEvent.TYPE);
	}
}
